package com.projects.modular.realty.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.projects.modular.realty.entity.Child;
import com.projects.modular.realty.mapper.ChildMapper;
import com.projects.modular.realty.model.params.ChildParam;
import com.projects.modular.realty.service.ChildService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 家庭成员服务实现类
 *
 * @author 曾大顺
 * @since 2025-02-11
 */
@Service
public class ChildServiceImpl extends ServiceImpl<ChildMapper, Child> implements ChildService {

    @Override
    public void add(ChildParam childParam) {
        Child child = new Child();
        BeanUtils.copyProperties(childParam, child);
        LambdaQueryWrapper<Child> childWrapper = new LambdaQueryWrapper<>();
        childWrapper.eq(Child::getUserId, child.getUserId());
        List<Child> childrenList = this.baseMapper.selectList(childWrapper);
        // 如果查询出该用户之前未添加过孩子  则将首次添加的孩子状态改为激活
        if (CollectionUtils.isEmpty(childrenList)) {
            child.setActiveState("1");
        } else {
            child.setActiveState("0");
        }
        // 将param中的属性复制到entity中
//        child.setUserId(childParam.getUserId());
//        child.setChildName(childParam.getChildName());
//        child.setChildBirthday(childParam.getChildBirthday());
//        child.setChildGender(childParam.getChildGender());
//        child.setChildWeight(childParam.getChildWeight());
//        child.setChildHeight(childParam.getChildHeight());
//        child.setDiseaseDiagnosis(childParam.getDiseaseDiagnosis());
         this.save(child);
    }

    @Override
    public void deleteById(String childId) {
        this.removeById(childId);
    }

    @Override
    public void update(ChildParam childParam) {
        Child child = this.getById(childParam.getChildId());
        if (child != null) {
            BeanUtils.copyProperties(childParam, child);
//            child.setUserId(childParam.getUserId());
//            child.setChildName(childParam.getChildName());
//            child.setChildBirthday(childParam.getChildBirthday());
//            child.setChildGender(childParam.getChildGender());
//            child.setChildWeight(childParam.getChildWeight());
//            child.setChildHeight(childParam.getChildHeight());
//            child.setDiseaseDiagnosis(childParam.getDiseaseDiagnosis());
            this.updateById(child);
        }
    }



    /**
     * 激活成员
     *
     * @param childId 孩子ID
     * @return 激活结果
     */
    @Override
    public ResponseData updateActive(String childId){
        Child child = this.getById(childId);
        if (child != null) {
            // 先把所有成员的激活状态改为未激活
            String userId = child.getUserId();
            UpdateWrapper<Child> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Child::getUserId,userId)
                    .set(Child::getActiveState,"0");
            this.update(updateWrapper);
            // 再把当前孩子的激活状态改为已激活
            child.setActiveState("1");
            this.updateById(child);
            return ResponseData.success("激活成功");
        }
        return ResponseData.error("未找到该成员");
    }


    @Override
    public Child getById(String childId) {
        return this.baseMapper.selectById(childId);
    }


    @Override
    public List<Child> getByUserId(String userId) {
        return this.baseMapper.selectByUserId(userId);
    }
}
